<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />
    <!-- 添加Font Awesome图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        /* 自定义提示框样式 */
        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 20px;
            border-radius: 4px;
            color: white;
            font-weight: bold;
            display: flex;
            align-items: center;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            z-index: 1000;
            transform: translateX(150%);
            transition: transform 0.4s ease-in-out;
        }
        .notification.show {
            transform: translateX(0);
        }
        .notification i {
            margin-right: 10px;
            font-size: 20px;
        }
        .notification.success {
            background-color: #4CAF50;
        }
        .notification.error {
            background-color: #f44336;
        }
    </style>
</head>

<body>
    <div class="nav">
        <img src="pic/logo2.jpg" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>
    <div class="content-edit">
        <div class="push">
            <input type="hidden" id="blogId">
            <input type="text" name="" id="title">
            <input type="button" value="更新文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content">##在这里写下一篇博客</textarea>
        </div>
    </div>

    <!-- 提示框组件 -->
    <div id="notification" class="notification">
        <i id="notificationIcon" class="fas"></i>
        <span id="notificationMessage"></span>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">

        // $(function () {
        //     var editor = editormd("editor", {
        //         width: "100%",
        //         height: "550px",
        //         path: "blog-editormd/lib/"
        //     });
        // });

        // 显示提示框
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            const icon = document.getElementById('notificationIcon');
            const messageElement = document.getElementById('notificationMessage');
            
            // 设置消息和图标
            messageElement.textContent = message;
            
            // 根据类型设置样式
            notification.className = 'notification';
            if (type === 'success') {
                notification.classList.add('success');
                icon.className = 'fas fa-check-circle';
            } else {
                notification.classList.add('error');
                icon.className = 'fas fa-exclamation-circle';
            }
            
            // 显示提示框
            setTimeout(() => {
                notification.classList.add('show');
            }, 10);
            
            // 3秒后隐藏
            setTimeout(() => {
                notification.classList.remove('show');
            }, 3000);
        }

        function submit() {
            $.ajax({
                type: "post",
                url: "/blog/update",
                contentType: "application/json",
                data: JSON.stringify({
                    "id": $("#blogId").val(),
                    "title": $("#title").val(),
                    "content": $("#content").val()
                }),
                success: function (result) {
                    if (result != null && result.code == 200 && result.data == true) {
                        showNotification("文章更新成功，即将返回列表页", "success");
                        setTimeout(function() {
                            location.href = "blog_list.html";
                        }, 2000);
                    } else {
                        showNotification(result.msg || "更新失败", "error");
                    }
                },
                error: function(err) {
                    showNotification("更新失败，请稍后重试", "error");
                }
            });
        }
        function getBlogInfo() {
            $.ajax({
                type: "get",
                url: "/blog/getBlogDetail" + location.search,
                success: function (result) {
                    if (result != null && result.code == 200 && result.data != null) {
                        $("#title").val(result.data.title);
                        // $("#content").html(result.data.content);
                        $("#blogId").val(result.data.id);
                        editormd("editor", {
                            width: "100%",
                            height: "550px",
                            path: "blog-editormd/lib/",
                            onload: function () {
                                this.watch();
                                this.setMarkdown(result.data.content);
                            }
                        });
                    }
                }
            });
        }
        getBlogInfo();
    </script>
</body>

</html>